package com.brainhealth.customer.service.impl;

import java.util.List;

import com.brainhealth.common.core.domain.entity.SysDictData;
import com.brainhealth.common.enums.AuditStatus;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.brainhealth.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.brainhealth.customer.domain.CustomerFeedbackImg;
import com.brainhealth.customer.mapper.CustomerFeedbackMapper;
import com.brainhealth.customer.domain.CustomerFeedback;
import com.brainhealth.customer.service.ICustomerFeedbackService;

/**
 * 会员反馈信息Service业务层处理
 * 
 * @author wolong
 * @date 2023-04-25
 */
@Service
public class CustomerFeedbackServiceImpl implements ICustomerFeedbackService 
{
    @Autowired
    private CustomerFeedbackMapper customerFeedbackMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询会员反馈信息
     * 
     * @param customerFeedbackId 会员反馈信息主键
     * @return 会员反馈信息
     */
    @Override
    public CustomerFeedback selectCustomerFeedbackByCustomerFeedbackId(Long customerFeedbackId)
    {
        return customerFeedbackMapper.selectCustomerFeedbackByCustomerFeedbackId(customerFeedbackId);
    }

    /**
     * 查询会员反馈信息列表
     * 
     * @param customerFeedback 会员反馈信息
     * @return 会员反馈信息
     */
    @Override
    public List<CustomerFeedback> selectCustomerFeedbackList(CustomerFeedback customerFeedback)
    {
        List<CustomerFeedback> lstData = customerFeedbackMapper.selectCustomerFeedbackList(customerFeedback);
        List<SysDictData> lstDicData = sysDictDataMapper.selectDictDataByType("feedback_status");
        for (CustomerFeedback feedback: lstData)
        {
            SysDictData dictData = lstDicData.stream().filter(item->item.getDictValue().equals(feedback.getStatus().toString())).findFirst().orElse(null);
            feedback.setStatusName(dictData.getDictLabel());
        }
        return lstData;
    }

    /**
     * 新增会员反馈信息
     * 
     * @param customerFeedback 会员反馈信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCustomerFeedback(CustomerFeedback customerFeedback)
    {
        customerFeedback.setCreateTime(DateUtils.getNowDate());
        int rows = customerFeedbackMapper.insertCustomerFeedback(customerFeedback);
        insertCustomerFeedbackImg(customerFeedback);
        return rows;
    }

    /**
     * 修改会员反馈信息
     * 
     * @param customerFeedback 会员反馈信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCustomerFeedback(CustomerFeedback customerFeedback)
    {
        customerFeedback.setUpdateTime(DateUtils.getNowDate());
        customerFeedbackMapper.deleteCustomerFeedbackImgByCustomerFeedbackId(customerFeedback.getCustomerFeedbackId());
        insertCustomerFeedbackImg(customerFeedback);
        return customerFeedbackMapper.updateCustomerFeedback(customerFeedback);
    }

    @Override
    public int customerFeedbackAudit(Long feedbackId)
    {
        CustomerFeedback customerFeedback = customerFeedbackMapper.selectCustomerFeedbackByCustomerFeedbackId(feedbackId);
        if (customerFeedback.getStatus().equals(AuditStatus.Audit))
        {
            customerFeedback.setStatus(AuditStatus.NoAudit);
        } else if (customerFeedback.getStatus().equals(AuditStatus.NoAudit))
        {
            customerFeedback.setStatus(AuditStatus.Audit);
        }
        int result = customerFeedbackMapper.updateCustomerFeedback(customerFeedback);
        return result;
    }

    /**
     * 批量删除会员反馈信息
     * 
     * @param customerFeedbackIds 需要删除的会员反馈信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerFeedbackByCustomerFeedbackIds(Long[] customerFeedbackIds)
    {
        customerFeedbackMapper.deleteCustomerFeedbackImgByCustomerFeedbackIds(customerFeedbackIds);
        return customerFeedbackMapper.deleteCustomerFeedbackByCustomerFeedbackIds(customerFeedbackIds);
    }

    /**
     * 删除会员反馈信息信息
     * 
     * @param customerFeedbackId 会员反馈信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerFeedbackByCustomerFeedbackId(Long customerFeedbackId)
    {
        customerFeedbackMapper.deleteCustomerFeedbackImgByCustomerFeedbackId(customerFeedbackId);
        return customerFeedbackMapper.deleteCustomerFeedbackByCustomerFeedbackId(customerFeedbackId);
    }

    /**
     * 新增会员反馈信息图片信息
     * 
     * @param customerFeedback 会员反馈信息对象
     */
    public void insertCustomerFeedbackImg(CustomerFeedback customerFeedback)
    {
        List<CustomerFeedbackImg> customerFeedbackImgList = customerFeedback.getCustomerFeedbackImgList();
        Long customerFeedbackId = customerFeedback.getCustomerFeedbackId();
        if (StringUtils.isNotNull(customerFeedbackImgList))
        {
            List<CustomerFeedbackImg> list = new ArrayList<CustomerFeedbackImg>();
            for (CustomerFeedbackImg customerFeedbackImg : customerFeedbackImgList)
            {
                customerFeedbackImg.setCustomerFeedbackId(customerFeedbackId);
                list.add(customerFeedbackImg);
            }
            if (list.size() > 0)
            {
                customerFeedbackMapper.batchCustomerFeedbackImg(list);
            }
        }
    }
}
